package org.jeecg.modules.wangyue.controller.upload;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.wangyue.constant.CommonConstant;
import org.jeecg.modules.wangyue.utils.FileUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * 管理端上传功能
 */
@Slf4j
@Api(tags="管理端上传功能")
@RestController
@RequestMapping("/wangyue/sys/sysUpload")
public class SystemUploadController {


    /**
     * 上传文件
     * @param file          上传文件
     * @param uploadType    上传类型
     * @param oldFileUrl    旧文件的url
     * @return
     */
    @AutoLog(value = "上传文件")
    @ApiOperation(value="上传文件", notes="上传文件")
    @PostMapping(value = "/uploadFile")
    public Result<?> uploadFile(MultipartFile file,
                                @RequestParam(name="uploadType",required = true) String uploadType,
                                @RequestParam(name="oldFileUrl",required = false) String oldFileUrl){
        if(oConvertUtils.isEmpty(uploadType) || oConvertUtils.isEmpty(file)){
            return Result.error("必输项不能为空");
        }

        String addPath = File.separator + UploadPath.common + File.separator + UploadPath.sys; // 值为: /upload/sys
        if(UploadPath.servicePackage.equals(uploadType)){ //服务套餐
            addPath = addPath + File.separator + UploadPath.servicePackagePath;

        }else if(UploadPath.banner.equals(uploadType)){ //轮播图
            addPath = addPath + File.separator + UploadPath.bannerPath;

        }else if(UploadPath.serviceType.equals(uploadType)){ //服务类型
            addPath = addPath + File.separator + UploadPath.serviceTypePath;

        }
        String url = FileUtil.uploadNewAndDelOld(file, oldFileUrl, addPath);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("url", url);
        return Result.OK(jsonObject);
    }


    /**
     * 删除文件
     * @param fileUrl 文件路径
     * @return
     */
    @AutoLog(value = "删除文件")
    @ApiOperation(value="删除文件", notes="删除文件")
    @PostMapping(value = "/delFile")
    public Result<?> delFile(@RequestParam(name="fileUrl",required = true) String fileUrl){
        if(oConvertUtils.isEmpty(fileUrl)){
            return Result.error("上传文件不能为空");
        }

        //默认头像不需要删除，直接响应即可
        if(CommonConstant.defaultHeadUrl.equals(fileUrl)){
            return Result.OK("文件删除成功");
        }

        //删除文件
        if(FileUtil.delFile(fileUrl)){
            return Result.OK("文件删除成功");
        }else {
            return Result.error("文件删除失败");
        }
    }


}
